package com.qlmjx.sevalopos;

import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.alibaba.fastjson.JSON;
import com.qlmjx.sevalopos.Bean.ManageAllInfo;
import com.qlmjx.sevalopos.Bean.ManageHalfInfo;
import com.qlmjx.sevalopos.Bean.OrederBean;
import com.qlmjx.sevalopos.Bean.ViewPagerBean;
import com.qlmjx.sevalopos.View.BaseDialog;
import com.qlmjx.sevalopos.View.HomeBannerHolderView;
import com.qlmjx.sevalopos.View.PaySuccessDialog;
import com.qlmjx.sevalopos.adapter.UnpaidCustomerAdapter;
import com.qlmjx.sevalopos.base.BaseEvent;
import com.qlmjx.sevalopos.event.AdvEvent;
import com.qlmjx.sevalopos.event.PaySEvent;
import com.qlmjx.sevalopos.event.UnpaidCustomerShopEvent;
import com.qlmjx.sevalopos.event.UnpaidEvent;
import com.qlmjx.sevalopos.https.HttpAddress;
import com.qlmjx.sevalopos.https.RequestParams;
import com.qlmjx.sevalopos.utils.Screensaver;
import com.universal.frame.banner.CBViewHolderCreator;
import com.universal.frame.banner.ConvenientBanner;
import com.universal.frame.banner.OnItemClickListener;
import com.universal.frame.generalutils.GeneralUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import okhttp3.Request;

/**
 * Created by sa on 2018/7/20.
 */

public class Customer2Activity extends Presentation implements ResultCallBack {

    //广告轮播
    private ConvenientBanner<ViewPagerBean> slider;
    private ConvenientBanner<ViewPagerBean> all_slider;
    private List<ViewPagerBean> ads = new ArrayList<ViewPagerBean>();
    private List<ViewPagerBean> all_ads = new ArrayList<ViewPagerBean>();
    private ListView lv_shop;
    private LinearLayout ll_all;
    private RelativeLayout rl_zkl, rl_zkj;
    private List<OrederBean> list = new ArrayList<OrederBean>();
    private UnpaidCustomerAdapter unpaidCustomerAdapter;
    private int type;
    private TextView tv_js, tv_yj, tv_hyj, tv_zkj, tv_khmc, tv_hyid, tv_yf, tv_zk;
    private Screensaver dialogcreensaver;
    private PaySuccessDialog paySuccessDialog;

    public Customer2Activity(Context outerContext, Display display) {
        super(outerContext, display);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_customer2);
        rl_zkl = findViewById(R.id.rl_zkl);
        ll_all = findViewById(R.id.ll_all);
        slider = findViewById(R.id.slider);
        all_slider = findViewById(R.id.all_slider);
        lv_shop = findViewById(R.id.lv_shop);
        tv_js = findViewById(R.id.tv_js);
        tv_yj = findViewById(R.id.tv_yj);
        tv_hyj = findViewById(R.id.tv_hyj);
        tv_zkj = findViewById(R.id.tv_zkj);
        tv_khmc = findViewById(R.id.tv_khmc);
        tv_hyid = findViewById(R.id.tv_hyid);
        tv_yf = findViewById(R.id.tv_yf);
        tv_zk = findViewById(R.id.tv_zk);
        rl_zkj = findViewById(R.id.rl_zkj);
        EventBus.getDefault().register(this);
        setViewpager();
        setAllViewpager();
        setShopList();
        getAllInfo();
        getHalfInfo();
        dialogcreensaver = new Screensaver(5000); //定时5秒
        dialogcreensaver.setOnTimeOutListener(new Screensaver.OnTimeOutListener() {
            @Override
            public void onTimeOut(Screensaver screensaver) {
                paySuccessDialog.dismiss();
            }
        }); //监听
    }

    /**
     * 初始副屏的商品list
     */
    public void setShopList() {
        list.clear();
        unpaidCustomerAdapter = new UnpaidCustomerAdapter(getContext(), list);
        lv_shop.setAdapter(unpaidCustomerAdapter);
    }
    //全屏图
    public void getAllInfo() {
        OKHttpUtils.postAsync(getContext()
                , HttpAddress.SERVICE_PIC, ""
                , new RequestParams(getContext()).getPicParams("1")
                , this
                , false, 1);
    }
    //半屏
    public void getHalfInfo() {
        OKHttpUtils.postAsync(getContext()
                , HttpAddress.SERVICE_PIC, ""
                , new RequestParams(getContext()).getPicParams("2")
                , this
                , false, 2);
    }
    /**
     * 初始化广告页
     */
    public void setViewpager() {

        this.slider.setPages(new CBViewHolderCreator<HomeBannerHolderView>() {

            @Override
            public HomeBannerHolderView createHolder() {
                return new HomeBannerHolderView();
            }

        }, ads).setPageIndicator(new int[]{
                R.mipmap.dot_normal,
                R.mipmap.dot_focused
        }).startTurning(8000);

        this.slider.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(int position) {

            }
        });
    }

    /**
     * 初始化广告页
     */
    public void setAllViewpager() {

        this.all_slider.setPages(new CBViewHolderCreator<HomeBannerHolderView>() {

            @Override
            public HomeBannerHolderView createHolder() {
                return new HomeBannerHolderView();
            }

        }, all_ads).setPageIndicator(new int[]{
                R.mipmap.dot_normal,
                R.mipmap.dot_focused
        }).startTurning(8000);

        this.all_slider.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(int position) {

            }
        });
    }

    @Subscribe
    public void onEventMainThread(BaseEvent event) throws IOException {
//        super.onEventMainThread(event);
        if (event instanceof AdvEvent) {
            type = ((AdvEvent) event).getType();
            if (type == 1 && "8".equals(all_slider.getVisibility() + "")) {
                all_slider.setVisibility(View.VISIBLE);
                ll_all.setVisibility(View.GONE);
            }
            if (type == 2 && "0".equals(all_slider.getVisibility() + "")) {
                all_slider.setVisibility(View.GONE);
                ll_all.setVisibility(View.VISIBLE);
            }
        }
        if (event instanceof UnpaidCustomerShopEvent) {
            list.clear();
            list.addAll(((UnpaidCustomerShopEvent) event).getList());
            unpaidCustomerAdapter.notifyDataSetChanged();
        }
        if (event instanceof UnpaidEvent) {
            tv_yj.setText("￥" + ((UnpaidEvent) event).getYj());
            if ("1".equals(((UnpaidEvent) event).getZkl())) {
                rl_zkj.setVisibility(View.GONE);
                rl_zkl.setVisibility(View.GONE);
            } else {
                rl_zkj.setVisibility(View.VISIBLE);
                rl_zkl.setVisibility(View.VISIBLE);
                tv_hyj.setText("￥" + ((UnpaidEvent) event).getZkj());
                tv_zk.setText(((UnpaidEvent) event).getZkl());
            }
            tv_zkj.setText("￥" + ((UnpaidEvent) event).getYf());
            tv_yf.setText("￥" + ((UnpaidEvent) event).getYfj());
            tv_khmc.setText(((UnpaidEvent) event).getName());
        }
        if (event instanceof PaySEvent) {
            if (((PaySEvent) event).getType()==2){
                showSuccess();
                dialogcreensaver.start();
            }
        }
    }


    @Override
    public void onDisplayRemoved() {
        super.onDisplayRemoved();
        EventBus.getDefault().unregister(this);
    }

    //把String转化为double
    public static double convertToDouble(String number, double defaultValue) {
        if (TextUtils.isEmpty(number)) {
            return defaultValue;
        }
        try {
            return Double.parseDouble(number);
        } catch (Exception e) {
            return defaultValue;
        }

    }
    private void showSuccess() {
        paySuccessDialog = new PaySuccessDialog(getContext(), "提示", "恭喜您支付成功！", R.style.MyDialogStyle, new BaseDialog.LeaveMyDialogListener() {

            @Override
            public void onClick(View view) {


            }

        });
        paySuccessDialog.show();

    }

    @Override
    public void onSuccess(String response, int flag) {
        try {
            if (flag == 1) {
                getAllPicData(response);
            }
            if (flag == 2) {
                getHalfPicData(response);
            }

        } catch (Exception e) {
            GeneralUtil.tryShow(getContext(), e);
        }
    }
    private void getAllPicData(String response) {
        ManageAllInfo info = JSON.parseObject(response, ManageAllInfo.class);
        if (info.list.size()>0){
            all_ads.clear();
            all_ads.addAll(info.list);
            this.all_slider.notifyDataSetChanged();
        }else {
            ToastUtil.showToast(getContext(),"暂未设置图片");
        }

    }
    private void getHalfPicData(String response) {
        ManageHalfInfo info = JSON.parseObject(response, ManageHalfInfo.class);
        if (info.list.size()>0){
            ads.clear();
            ads.addAll(info.list);
            this.slider.notifyDataSetChanged();
        }else {
            ToastUtil.showToast(getContext(),"暂未设置图片");
        }
    }
    @Override
    public void onFailure(Request request, IOException e, int flag) {

    }
}
